import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { AppService } from './app.service';
import { AaaGuard } from './aaa.guard';
import { Aaa } from './aaa.decorator';
import { MapTestInterceptor } from './map-test.interceptor';

import { Logger } from 'winston';
import { TimeoutInterceptor } from './timeout.interceptor';
import { Ooo } from './dto/Ooo';
import { FileInterceptor } from '@nestjs/platform-express';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';

@Controller()
@UseInterceptors(MapTestInterceptor)
export class AppController {
  constructor(
    private readonly appService: AppService,
    @Inject('person') private readonly person: { name: string; age: number },
  ) {}

  @Inject(WINSTON_MODULE_PROVIDER)
  private logger: Logger;

  @Get()
  getHello(): string {
    this.logger.info(AppController.name);
    return this.appService.getHello();
  }

  @Get('/person')
  @Aaa('admin')
  @UseGuards(AaaGuard)
  getPerson() {
    return this.person;
  }

  @Get('timeout')
  @UseInterceptors(TimeoutInterceptor)
  async timeout() {
    await new Promise((resolve) => setTimeout(resolve, 4000));
    return 'ddd';
  }

  @Post('ooo')
  ooo(@Body() obj: Ooo) {
    console.log(Ooo);
    return obj;
  }

  @Post('upload')
  @UseInterceptors(
    FileInterceptor('file', {
      dest: 'uploads',
    }),
  )
  uploadFile(@UploadedFile() file: Express.Multer.File, @Body() body) {
    console.log('body', body);
    console.log('file', file);
  }

  @Post('log')
  log(@Body() body) {
    console.log(body);
  }
}
